<?php
if (!defined('IN_D_ADMIN')) die("Error 404");
$total_song_per_page=20;
if($_GET['mode']=='add'){
if (!$_POST) {
	?>
	<script>
	function docheckall(){
		   for ( i=0;i < document.list_focus.elements.length ; i++ ){
				 if ( document.list_focus.chkall.checked==true ){
					  document.list_focus.elements[i].checked = true;
				 } else {
					  document.list_focus.elements[i].checked  = false;
				 }
		   }
	}
	function check_song(total){
		if(document.getElementById("select_song_all").checked==true){
			val=true;
		}
		else{
			val=false;	
		}				
			for(i=0;i<=total;i++){
				document.getElementById("select_song["+i+"]").checked=val;	
			}					
	}
</script>
	<form method=post name="list_focus">
	<table class=border cellpadding=2 cellspacing=0 width=95%>
	<tr><td colspan=2 class=title align=center><?=$lang_acp['manager_focus']?></td></tr>

	<?php
	if($_GET['kind_focus']=='album') $ju=1;
	elseif($_GET['kind_focus']=='movie') $ju=2;
	if($_GET['kind_focus']=='album' || $_GET['kind_focus']=='movie'){
	?>
	<tr><td colspan=2 class=title align=center>Select all<input class=checkbox type=checkbox name=chkall id=chkall onclick=docheckall() value=checkall>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $lang_acp['list_focus_multimedia']?></td></tr>
	<?php
		$sql=$DB->query("select F.*,multi_name from {$conf['prefix']}focus F left join {$conf['prefix']}multimedia MU on F.focus_multi=MU.multi_id where multi_type={$ju} and multi_id in(select focus_multi from {$conf['prefix']}focus where focus_multi<>'') order by focus_id desc");
			while($focus=$DB->fetch_row($sql)){
		?>
		<tr>
		<td class=fr width=50%><input type="checkbox" name="del[]"  value="<?php echo $focus['focus_id']?>"/>&nbsp;<a href="index.php?act=focus&mode=edit&focus_id=<?php echo $focus['focus_id']?>&kind_focus=<?php echo $_GET['kind_focus']?>"><?php echo $focus['multi_name']?></a></td><td class=fr_2><?php echo acp_focus($focus['focus_type'],$_GET['kind_focus'])?></td>
		</tr>
		<?php
			}
	}
	elseif($_GET['kind_focus']=='song'){
	?>
		<tr><td colspan=2 class=title align=center>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $lang_acp['list_focus_song']?></td></tr>
	<?php
		$per_page=15;
		if (!$pg) $pg = 1;
		$sql=$DB->query("select F.*,m_id,m_title from {$conf['prefix']}focus F left join {$conf['prefix']}media M on F.focus_media=M.m_id where m_id in(select focus_media from {$conf['prefix']}focus where focus_media<>'') order by focus_id desc");
		$tt=$DB->num_rows($sql);
			while($focus=$DB->fetch_row($sql)){
		?>
		<tr>
		<td class=fr width=30%><input type="checkbox" name="select_focus[]"  value="<?php echo $focus['focus_id']?>"/>&nbsp;&nbsp;<input type="text" name="stt[]" size=5 value="<?php echo $focus['focus_stt']?>"/>&nbsp;&nbsp;<?php echo $focus['m_title']?></td><td class=fr_2><?php echo acp_focus($focus['focus_type'],$_GET['kind_focus'])?></td>
		</tr>
		<?php
			}
	}
	if($_GET['kind_focus']=='album' || $_GET['kind_focus']=='movie'){	
		if($_GET['kind_focus']=='album') $title=$lang_acp['album'];
		elseif($_GET['kind_focus']=='movie') $title=$lang_acp['movie'];
		?>
	<tr><td colspan=2 class=title align=center><?php echo $lang_acp['add_focus']?></td></tr>
		<tr>
		<td class=fr width=30%><?php echo $title?></td><td class=fr_2><?php echo acp_multi_list('','','',$_GET['kind_focus'])?></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['type_focus']?></td><td class=fr_2><?php echo acp_select_focus($_GET['kind_focus'])?></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['desc_focus']?></td><td class=fr_2><textarea name="desc_focus" cols="50" rows="5"></textarea></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['img_focus']?></td><td class=fr_2><input type="text" name="img_focus" size=50/></td>
		</tr>
		<?php
	}
	elseif($_GET['kind_focus']=='song'){
	?>
	<tr><td colspan=2 class=title align=center>Select all&nbsp;&nbsp;<input class=checkbox type=checkbox name=select_song_all id=select_song_all onclick='check_song(<?php echo $total_song_per_page?>);' value=1>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $lang_acp['add_focus']?></td></tr>
	<?php
		if (!$pg) $pg = 1;
		$qr_song=$DB->query("select * from {$conf['prefix']}media where m_multimedia=0 OR m_multimedia IN (select multi_id from {$conf['prefix']}multimedia where multi_type=1) order by m_id desc limit ".($pg-1)*$total_song_per_page.",".$total_song_per_page);
		$tt_song=$DB->num_rows($DB->query("select * from {$conf['prefix']}media where m_multimedia=0 OR m_multimedia IN (select multi_id from {$conf['prefix']}multimedia where multi_type=1)"));
		$n=0;
			while($song=$DB->fetch_row($qr_song)){
			$type_media=$DB->fetch_row($DB->query("SELECT player_name FROM ".$conf['prefix']."player WHERE player_id=".$song['m_type']));
					$media_type_name=$type_media['player_name'];
		?>
		<tr>
		<td class="fr" width="3%"><input type="checkbox" name="select_song[]" id="select_song[<?php echo $n?>]" value="<?php echo $song['m_id']?>" />&nbsp;&nbsp;<?php echo $media_type_name?></td><td class=fr><?php echo $song['m_title']?></td>
		</tr>
		<?php
			$n++;
			}
		echo "<tr><td colspan=3>".admin_viewpages($tt_song,$total_song_per_page,$pg)."</td></tr>";
	}
	?><input name="kind_focus" value="<?php echo $_GET['kind_focus']?>" type="hidden" />
	<tr><td class=fr colspan=2 align=center><input type=submit name=delete_focus class=submit value='Delele'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit name=submit_add class=submit value='Submit'><?php echo ($_GET['kind_focus']=='song')?"&nbsp;&nbsp;".acp_select_focus('song'):""?></td></tr>
	
	</table>
	</form>
	<?php
}
else {
if($_POST['kind_focus']=='album') $multi_type=1;
elseif($_POST['kind_focus']=='movie') $multi_type=2;
		if ($_POST['multimedia'] && $_POST['submit_add']) {
			$ro_type=$_POST['type_focus'];
			$ro_multi=$_POST['multimedia'];
			$ro_img=leech_img(trim($_POST['img_focus']),"focus_multi");
			$ro_desc=$func->htmlchars(stripslashes(trim($_POST['desc_focus'])));
			$DB->query("INSERT INTO {$conf['prefix']}focus(focus_type,focus_multi,focus_img,focus_desc) VALUES ('{$ro_type}','{$ro_multi}','{$ro_img}','{$ro_desc}')");
			echo "Đã thêm xong <meta http-equiv='refresh' content='1;url=$link'>";
		}
		if ($_POST['select_song'] && $_POST['submit_add']) {
			$ro_type=$_POST['type_focus'];
			$arr=$_POST['select_song'];
			foreach($arr as $media_id){
				$DB->query("INSERT INTO {$conf['prefix']}focus(focus_type,focus_media) VALUES ('{$ro_type}','{$media_id}')");
			}
			echo "Đã thêm xong <meta http-equiv='refresh' content='1;url=$link'>";
		}
		if ($_POST['stt'] && $_POST['submit_add'] && $_POST['select_focus']) {
			$stt=$_POST['stt'];
			$arr=$_POST['select_focus'];
			for($i=0;$i<=count($arr)-1;$i++){
				$DB->query("UPDATE {$conf['prefix']}focus SET focus_stt=".$stt[$i]." where focus_id=".$arr[$i]);
			}
			echo "Đã sửa xong <meta http-equiv='refresh' content='1;url=$link'>";
		}
		if($_POST['delete_focus'] && ($_POST['kind_focus']=='album' || $_POST['kind_focus']=='movie')){
			$arr=$_POST['del'];
			$in_sql = implode(',',$arr);
			$func->del_local_img("focus_multi",$in_sql);
			$qr_del=$DB->query("delete  from {$conf['prefix']}focus where focus_id in({$in_sql})");
			if($qr_del) echo "Đã xóa xong <meta http-equiv='refresh' content='1;url=$link'>";
			else echo "ko xóa đc";
		}
		if ($_POST['select_focus'] && $_POST['delete_focus']) {
			$arr=$_POST['select_focus'];
			$in_sql = implode(',',$arr);
			$qr_del=$DB->query("delete  from {$conf['prefix']}focus where focus_id in({$in_sql})");
			if($qr_del) echo "Đã xóa xong <meta http-equiv='refresh' content='1;url=$link'>";
			else echo "ko xóa đc";
		}
		
		
}
}
elseif($_GET['mode']=='edit'){

	if($_POST['submit_edit']){
			$id=$_POST['focus_id'];
			$ro_type=$_POST['type_focus'];
			$ro_multi=$_POST['multimedia'];
			if(trim($_POST['img_focus']))
			$ro_img=leech_img(trim($_POST['img_focus']),"focus_multi");
			$ro_desc=$func->htmlchars(stripslashes(trim($_POST['desc_focus'])));
			if($_POST['kind_focus']=='album') $multi_type=1;
			elseif($_POST['kind_focus']=='movie') $multi_type=2;
			if($ro_img)
			$DB->query("update {$conf['prefix']}focus set focus_multi={$ro_multi},focus_img='{$ro_img}',focus_desc='{$ro_desc}',focus_type='{$ro_type}' where focus_id={$id}");
			else
			$DB->query("update {$conf['prefix']}focus set focus_multi='{$ro_multi}',focus_desc='{$ro_desc}',focus_type='{$ro_type}' where focus_id={$id}");
			echo "Đã sửa xong <meta http-equiv='refresh' content='1;url=index.php?act=focus&mode=add&kind_focus={$_POST['kind_focus']}'>";
	
	}
	else{
	$sql=$DB->query("select F.*,multi_name from {$conf['prefix']}focus F left join {$conf['prefix']}multimedia MU on F.focus_multi=MU.multi_id where focus_id='{$_GET['focus_id']}'");
	$f=$DB->fetch_row($sql);
?>
	<form method=post name="edit_focus">
	<table class=border cellpadding=2 cellspacing=0 width=95%>
	<tr><td colspan=2 class=title align=center><?=$lang_acp['edit_focus']?></td></tr>
		<tr>
		<td class=fr width=30%><?php echo $title?></td><td class=fr_2><?php echo acp_multi_list($f['focus_multi'],'','',$_GET['kind_focus'])?></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['type_focus']?></td><td class=fr_2><?php echo acp_select_focus($_GET['kind_focus'])?></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['desc_focus']?></td><td class=fr_2><textarea name="desc_focus" cols="50" rows="5"><?=$func->unhtmlchars($f['focus_desc'])?></textarea></td>
		</tr>
		<tr>
		<td class=fr width=30%><?php echo $lang_acp['img_focus']?></td><td class=fr_2><img src="<?php echo $conf['web_url']."/".$conf['focus_img']."/".$f['focus_img']?>" border="0" width="300" /><br /><input type="text" name="img_focus" size=50/></td>
		</tr>
<input name="kind_focus" value="<?php echo $_GET['kind_focus']?>" type="hidden" />
<input name="focus_id" value="<?php echo $_GET['focus_id']?>" type="hidden" />
	<tr><td colspan=2 class=title align=center><input type=submit name=submit_edit class=submit value=Edit></td></tr>
	</table></form>
<?php		
	}

}
?>